package com.ref.entity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author wzm
 * @create 2023-02-14 10:08
 */
public class ReflectTest6 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        //获取字节码文件对象
        Class clazz = Class.forName("com.ref.entity.Student");
        
        // 获取当前类与父类的public修饰方法
        Method[] methods = clazz.getMethods();
        for(Method method:methods){
            System.out.println("method = " + method);
        }

        System.out.println("-------------------");
        Method[] declaredMethods = clazz.getDeclaredMethods();
        for(Method method:declaredMethods){
            System.out.println("method = " + method);
        }
        
        //获取某个空参方法 public 1 方法名  2参数类型
        Method sleep = clazz.getMethod("sleep");
        System.out.println("sleep = " + sleep);


        
        //有参方法
        Method eat = clazz.getMethod("eat", String.class);
        System.out.println("eat = " + eat);

        Student student = new Student();

        //调用方法
        String result = (String) eat.invoke(student, "猪脚米线");
        System.out.println("参数是："+result);


        //私有的方法
        Method sayHello = clazz.getDeclaredMethod("sayHello", String.class);
        sayHello.setAccessible(true);
        //对象  参数
        sayHello.invoke(student, "刘德华");

    }
}
